﻿using System;

namespace Alfa.Units {
	public struct Kelvin : ITemperatureUnit, IComparable<Kelvin>, IEquatable<Kelvin> {
		private decimal degrees;
		public Kelvin(decimal Degrees) {
			this.degrees = Degrees;
		}

		public static explicit operator Celsius(Kelvin kelvin) {
			return new Celsius(kelvin.degrees - 273.15m);
		}

		public static explicit operator Fahrenheit(Kelvin kelvin) {
			return new Fahrenheit(kelvin.degrees * 9m / 5m - 459.67m);
		}

		public static explicit operator Rankine(Kelvin kelvin) {
			return new Rankine(kelvin.degrees * 9m / 5m);
		}

		public static implicit operator decimal(Kelvin kelvin) {
			return kelvin.degrees;
		}

		decimal ITemperatureUnit.Degrees { get { return this.degrees; } }

		string ITemperatureUnit.Symbol { get { return "K"; } }

		public int CompareTo(object obj) {
			if (obj is ITemperatureUnit) {
				return ((ITemperatureUnit)this).CompareTo((ITemperatureUnit)obj);
			} else
				throw new InvalidCastException(obj.GetType( ).Name + " does not implement ITemperatureUnit");
		}

		int IComparable<ITemperatureUnit>.CompareTo(ITemperatureUnit other) {
			return this.degrees.CompareTo(((Kelvin)other).degrees);
		}

		bool IEquatable<ITemperatureUnit>.Equals(ITemperatureUnit other) {
			return this.degrees.Equals(((Kelvin)other).degrees);
		}

		int IComparable<Kelvin>.CompareTo(Kelvin other) {
			return this.degrees.CompareTo(other.degrees);
		}

		bool IEquatable<Kelvin>.Equals(Kelvin other) {
			return this.degrees.Equals(other.degrees);
		}
	}
}
